﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace CATLTempUpdate
{
    class MyCommand
    {
        public static List<string> SysErroList = new List<string>();
        public static string CtrlMod = "off";
        public static string SpeakMod = "1";
        public static string StageAlarmSt = "off";
        public static List<string> frieStage = new List<string>();


        public MyCommand()
        {
            delWriteLogTxt = new DelWriteLogTxt(WriteLogTxt);
        }
        public static byte[] GetSHA1Byte(byte[] bytes_sha1_in)
        {
            SHA1 sha1 = new SHA1CryptoServiceProvider();
            byte[] bytes_sha1_out = sha1.ComputeHash(bytes_sha1_in);
            return bytes_sha1_out;
        }

        public static string GetSHA1Str(byte[] bytes_sha1_in)
        {
            byte[] bytes_sha1_out = GetSHA1Byte(bytes_sha1_in);
            string str_sha1_out = BitConverter.ToString(bytes_sha1_out);
            return str_sha1_out;
        }

        public void ErroLog(string logMsg)
        {
            delWriteLogTxt.BeginInvoke("SYSERRO", logMsg, null, null);
        }

        delegate void DelWriteLogTxt(string logname, string logMsg);
        private DelWriteLogTxt delWriteLogTxt;
        //创建txt     将测定结果存到指定路径下
        public void WriteLogTxt(string logname, string logMsg)
        {
            try
            {
                string exportPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase; ; //生成CSV路径
                string NewFileName = DateTime.Now.ToString("yyyyMMdd") + logname + ".log";
                exportPath = exportPath + "\\" + logname + "\\";
                if (!Directory.Exists(exportPath))
                {
                    Directory.CreateDirectory(exportPath);
                }
                FileStream fs1 = new FileStream(exportPath + NewFileName, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);//创建写入文件 
                StreamWriter sw = new StreamWriter(fs1);
                //int length = logList.Count;
                //while (logList.Count>0)
                //{
                //    sw.WriteLine(logList[0]);//开始写入值
                //    logList.RemoveAt(0);
                //}
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd  HH:mm:ss"));
                sw.WriteLine(logMsg);
                sw.WriteLine();
                sw.Close();
                fs1.Close();
            }
            catch { }
        }
    }
}
